#install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
#install.packages("devtools")
library(devtools)
## Loading required package: usethis
devtools::install_github("rstudio-education/dsbox", force = TRUE)
## Downloading GitHub repo rstudio-education/dsbox@HEAD
## ── R CMD build ─────────────────────────────────────────────────────────────────
##
checking for file ‘/tmp/RtmpZXDTAd/remotes6fa4e5bdfc4/rstudio-education-dsbox-8fd2a47/DESCRIPTION’ ...
✔ checking for file ‘/tmp/RtmpZXDTAd/remotes6fa4e5bdfc4/rstudio-education-dsbox-8fd2a47/DESCRIPTION’
##
─ preparing ‘dsbox’:
##
checking DESCRIPTION meta-information ...
✔ checking DESCRIPTION meta-information
##
─ checking for LF line-endings in source and make files and shell scripts
##
─ checking for empty or unneeded directories
##
─ building ‘dsbox_0.1.1.tar.gz’
##
Warning: invalid uid value replaced by that for user 'nobody'
##
##
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library(dsbox)
#install.packages("ggridges")
library(ggridges)
#install.packages("gganimate")
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
#install.packages("janitor")
library(janitor)
##
## Attaching package: 'janitor'
##
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
#install.packages("stringr")
library(stringr) ## String manipulation
#install.packages("plotly")
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
#install.packages("usmap")
library(usmap)
library(ggplot2)
#install.packages("devtools")
devtools::install_github("UrbanInstitute/urbnmapr")
## Skipping install of 'urbnmapr' from a github remote, the SHA1 (ef9f4488) has not changed since last install.
## Use `force = TRUE` to force installation
library(urbnmapr)
#install.packages("leaflet")
library(leaflet) ## For leaflet interactive maps
#install.packages("sf")
library(sf) ## For spatial data
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
#install.packages("RColorBrewer")
library(RColorBrewer) ## For colour palettes
#install.packages("htmltools")
library(htmltools) ## For html
#install.packages("leafsync")
library(leafsync) ## For placing plots side by side
#install.packages("kableExtra")
library(kableExtra) ## Table output
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
#install.packages("stringr")
library(stringr) ## String manipulation
us_maps <- us_map(region = "states")
states_sf <- get_urbn_map(map = "states", sf = TRUE)
us_maps <- us_maps %>%
rename("State" = "full")
cod_landings <- read_csv("data/cod-landings.csv")
## Rows: 1196 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): State, NMFS Name, Collection, Scientific Name, Source
## dbl (2): Year, Tsn
## num (3): Pounds, Metric Tons, Dollars
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cod_landings <- cod_landings
names(cod_landings)[names(cod_landings) == "NMFS Name"] <- "Common_Name"
names(cod_landings)[names(cod_landings) == "Metric Tons"] <- "Metric_Tons"
names(cod_landings)[names(cod_landings) == "Dollars"] <- "US_Dollars"
names(cod_landings)[names(cod_landings) == "Scientific Name"] <- "Scientific_Name"
names(cod_landings)[names(cod_landings) == "Collection"] <- "Fishing_Types"
names(cod_landings)[names(cod_landings) == "Tsn"] <- "Taxonomic_Serial_Number"
names(cod_landings)[names(cod_landings) == "Source"] <- "Fishing_Companies"
colnames(cod_landings)
## [1] "Year" "State"
## [3] "Common_Name" "Pounds"
## [5] "Metric_Tons" "US_Dollars"
## [7] "Fishing_Types" "Scientific_Name"
## [9] "Taxonomic_Serial_Number" "Fishing_Companies"
glimpse(cod_landings)
## Rows: 1,196
## Columns: 10
## $ Year <dbl> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021…
## $ State <chr> "ALASKA", "CONNECTICUT", "CONNECTICUT", "DELAW…
## $ Common_Name <chr> "COD, PACIFIC", "COD, ATLANTIC", "COD, ATLANTI…
## $ Pounds <dbl> 330404171, 2277, 7921, 18, 1171, 47311, 120113…
## $ Metric_Tons <dbl> 149870, 1, 4, 0, 1, 21, 545, 166, 21, 21, 41, …
## $ US_Dollars <dbl> 116766003, 5031, NA, NA, NA, 128833, 2619925, …
## $ Fishing_Types <chr> "Commercial", "Commercial", "Recreational", "R…
## $ Scientific_Name <chr> "Gadus macrocephalus", "Gadus morhua", NA, NA,…
## $ Taxonomic_Serial_Number <dbl> 164711, 164712, 164712, 164712, 164712, 164712…
## $ Fishing_Companies <chr> "AKFIN", "ACCSP", "MRIP", "MRIP", "MRIP", "ACC…
cod_landings$Common_Name <- str_replace(cod_landings$Common_Name,"COD, PACIFIC", "Pacific_Cod")
cod_landings$Common_Name <- str_replace(cod_landings$Common_Name,"COD, ATLANTIC", "Atlantic_Cod")
cod_landings$Scientific_Name <- str_replace(cod_landings$Scientific_Name, "Gadus macrocephalus", "Gadus_macrocephalus")
cod_landings$Scientific_Name <- str_replace(cod_landings$Scientific_Name, "Gadus morhua", "Gadus_morhua")
cod_landings
## # A tibble: 1,196 × 10
## Year State Commo…¹ Pounds Metri…² US_Do…³ Fishi…⁴ Scien…⁵ Taxon…⁶ Fishi…⁷
## <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr>
## 1 2021 ALASKA Pacifi… 3.30e8 149870 1.17e8 Commer… Gadus_… 164711 AKFIN
## 2 2021 CONNECT… Atlant… 2.28e3 1 5.03e3 Commer… Gadus_… 164712 ACCSP
## 3 2021 CONNECT… Atlant… 7.92e3 4 NA Recrea… <NA> 164712 MRIP
## 4 2021 DELAWARE Atlant… 1.8 e1 0 NA Recrea… <NA> 164712 MRIP
## 5 2021 MAINE Atlant… 1.17e3 1 NA Recrea… <NA> 164712 MRIP
## 6 2021 MAINE Atlant… 4.73e4 21 1.29e5 Commer… Gadus_… 164712 ACCSP
## 7 2021 MASSACH… Atlant… 1.20e6 545 2.62e6 Commer… Gadus_… 164712 ACCSP
## 8 2021 MASSACH… Atlant… 3.65e5 166 NA Recrea… <NA> 164712 MRIP
## 9 2021 NEW HAM… Atlant… 4.63e4 21 NA Recrea… <NA> 164712 MRIP
## 10 2021 NEW HAM… Atlant… 4.58e4 21 1.25e5 Commer… Gadus_… 164712 ACCSP
## # … with 1,186 more rows, and abbreviated variable names ¹Common_Name,
## # ²Metric_Tons, ³US_Dollars, ⁴Fishing_Types, ⁵Scientific_Name,
## # ⁶Taxonomic_Serial_Number, ⁷Fishing_Companies
Q1 - How do the weights of recorded recreational and commercial Cod landings of the Atlantic and Pacific compare to one another between the years 1950 and 2021? Plan Q1 - Summarize the data through a ridgeline density plot of weight of landings, colored by “Fishing_Type” and faceted by “Common_Name”. Also include an animated plot of the same type to better show the fluctuation in landings every 10 years.
cod_landings <- cod_landings %>%
filter(Common_Name != "COD, TOOTHED" ) %>%
filter(Common_Name != "COD, ARCTIC" )
#plotly allows to get info on the gppah directly... still better moving
#exclude arctic and toothed cod?
#How to make it continuous like an actual area? these are discrete variables
# the years are not in order for the Pacific cod?
#how to see each year and change graduation for the pounds to make it easier?
#stacked bar chart better?
#add + customize labels
#Choose colors
# add cod moratorium in 1992?
Graph_01 <- cod_landings %>%
arrange(desc(Year)) %>%
ggplot(mapping = aes(
x = Year,
y = Pounds,
color = Common_Name)) +
geom_area(alpha=0.5) +
geom_line() +
scale_x_continuous(breaks = seq(0, 100, 1))+
facet_wrap( ~ Common_Name)
# if want to choose color --> geom_area(fill="#69b3a2", alpha=0.5) +
# geom_line(color="#69b3a2")
Graph_01 <- ggplotly(Graph_01)
## Warning: Removed 33 rows containing non-finite values (`stat_align()`).
Graph_01
cod_landings_total = cod_landings %>%
group_by(Year) %>%
mutate(Total_Pounds = sum(Pounds, na.rm = TRUE)) %>%
arrange(desc(Year))
cod_landings_total %>%
ggplot(mapping = aes(
x = Year,
y = Total_Pounds,
fill = Year)) +
geom_col()
us_maps <- us_maps %>%
mutate(State = tolower(State))
cod_landings <- cod_landings %>%
mutate(State = tolower(State))
cod_landings_commercial <- cod_landings %>%
na.omit(US_Dollars) %>%
rename(Commercial_Profit = US_Dollars)
semi_join(cod_landings_commercial, us_maps)
## Joining with `by = join_by(State)`
## # A tibble: 819 × 10
## Year State Commo…¹ Pounds Metri…² Comme…³ Fishi…⁴ Scien…⁵ Taxon…⁶ Fishi…⁷
## <dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr>
## 1 2021 alaska Pacifi… 3.30e8 149870 1.17e8 Commer… Gadus_… 164711 AKFIN
## 2 2021 connect… Atlant… 2.28e3 1 5.03e3 Commer… Gadus_… 164712 ACCSP
## 3 2021 maine Atlant… 4.73e4 21 1.29e5 Commer… Gadus_… 164712 ACCSP
## 4 2021 massach… Atlant… 1.20e6 545 2.62e6 Commer… Gadus_… 164712 ACCSP
## 5 2021 new ham… Atlant… 4.58e4 21 1.25e5 Commer… Gadus_… 164712 ACCSP
## 6 2021 new york Atlant… 3.95e3 2 1.22e4 Commer… Gadus_… 164712 ACCSP
## 7 2021 oregon Pacifi… 2.20e3 1 7.81e2 Commer… Gadus_… 164711 PACFIN
## 8 2021 rhode i… Atlant… 1.28e4 6 2.63e4 Commer… Gadus_… 164712 ACCSP
## 9 2021 washing… Pacifi… 6.94e4 32 3.79e4 Commer… Gadus_… 164711 PACFIN
## 10 2020 alaska Pacifi… 3.80e8 172520 1.66e8 Commer… Gadus_… 164711 AKFIN
## # … with 809 more rows, and abbreviated variable names ¹Common_Name,
## # ²Metric_Tons, ³Commercial_Profit, ⁴Fishing_Types, ⁵Scientific_Name,
## # ⁶Taxonomic_Serial_Number, ⁷Fishing_Companies
#cod_landings_commercial %>%
#group_by(State) %>%
# summarise("1950") %>%
#ggplot()+
# geom_sf(aes(fill = Commercial_Profit))